﻿using System;
using System.Runtime.InteropServices;   //   DllImport所在的名字空间

namespace Eflong.MonitoringLib.Common
{
    public enum VCS_MT_TYPES
    {
        VCS_MT_DEFAULT , VCS_MT_LOGIN , VCS_MT_LOGOUT ,
        VCS_MT_UPDATEDEFINITION , VCS_MT_UPDATEDATA ,
        VCS_MT_QUERYDEFINITION , VCS_MT_QUERYDATA , VCS_MT_QUERYRECORD ,
        VCS_MT_QUERYREPORT , VCS_MT_SAVEOPTIONS , VCS_MT_QUERYCOALMINEINFO ,
        VCS_MT_QUERYREPORTTEMPLET , VCS_MT_QUERYREPORTTEMPLETDATA
    };

    public struct VizClientServer
    {
        public const int VCS_MT_LOGIN = 1;
        public const int VCS_MT_LOGOUT = 2;
        public const int VCS_MT_UPDATEDEFINITION = 3;
        public const int VCS_MT_UPDATEDATA = 4;
        public const int VCS_MT_QUERYDEFINITION = 5;
        public const int VCS_MT_QUERYDATA = 6;
        public const int VCS_MT_QUERYRECORD = 7;

        public const int VCS_MT_QUERYREPORT = 8;
        public const int VCS_MT_SAVEOPTIONS = 9;
        public const int VCS_MT_QUERYCOALMINEINFO = 10;
        public const int VCS_MT_QUERYREPORTTEMPLET = 11;
        public const int VCS_MT_QUERYREPORTTEMPLETDATA = 12;

        public const int VCS_NT_SENDDEFINITION = 1;
        public const int VCS_NT_SENDDATA = 2;
        public const int VCS_NT_UPDATEDEFINITION = 3;
        public const int VCS_NT_UPDATEDATA = 4;
        public const int VCS_NT_CONNECTIONSTATUSCHANGED = 5;

        public const int VCS_MR_OK = 1;
        public const int VCS_MR_UNKNOWNMETHOD = 0;
        public const int VCS_MR_FAILURE = ( -1 );
        public const int VCS_MR_FAILTOCONNECT = ( -11 );
        public const int VCS_MR_FAILTODISCONNECT = ( -12 );
        public const int VCS_MR_FAILTOREQUEST = ( -21 );
        public const int VCS_MR_FAILTOACK = ( -22 );
        public const int VCS_MR_FAILTOLOGIN = ( -23 );
        public const int VCS_MR_FAILTOLOGOUT = ( -24 );

        public uint m_dwMethodType;
        public int m_nMethodAck;

        public byte[ ] GetByteArray( )
        {
            byte[ ] byteArr = new byte[ 8 ];
            int pos = 0;
            byte[ ] bytes;
            bytes = KWConvert.GetByteArrayFromUInt( m_dwMethodType );

            KWConvert.SetByteArrayToByteArray( bytes , ref byteArr , ref pos , 4 );

            bytes = KWConvert.GetByteArrayFromInt( m_nMethodAck );

            KWConvert.SetByteArrayToByteArray( bytes , ref byteArr , ref pos , 4 );

            return byteArr;
        }

        public void ConverFromByteArray( byte[ ] ba )
        {
            int pos = 0;
            m_dwMethodType = KWConvert.GetUIntFromByteArray( ba , ref pos );
            m_nMethodAck = KWConvert.GetIntFromByteArray( ba , ref pos );
        }
    }

    [Serializable]

    //[StructLayout(LayoutKind.Sequential)]
    public struct LoginStruct
    {
        private const int SIZE_USERID = 17;
        private const int SIZE_PASSWORD = 9;
        private const int SIZE_MACADDRESS = 13;
        private const int SIZE_LICENSEKEY = 49;
        private const int SIZE_OPTIONS = 228;
        private const int SIZE_SYSTEMTIME = 16;

        public const int VCS_MT_LOGIN = 1;
        public const int VCS_MT_LOGOUT = 2;
        public const int LOGIN_BRIDGE = 1;
        public const int LOGOUT_BRIDGE = 2;
        public const int LOGIN_CLIENT = 3;
        public const int LOGOUT_CLIENT = 4;
        public const int LOGIN_NETWORKMODE = 0;
        public const int LOGIN_TYPE = LOGIN_CLIENT;

        public uint m_dwMethodType;
        public int m_nMethodAck;

        [MarshalAs( UnmanagedType.ByValArray , SizeConst = SIZE_USERID )]
        public char[ ] m_szUserId;

        [MarshalAs( UnmanagedType.ByValArray , SizeConst = SIZE_PASSWORD )]
        public char[ ] m_szPassword;

        public int m_nType;
        public uint m_dwPermission;

        //Options m_Options;
        [MarshalAs( UnmanagedType.ByValArray , SizeConst = SIZE_OPTIONS )]
        public char[ ] m_Options;

        ///SYSTEMTIME m_stServer;
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = SIZE_SYSTEMTIME)]
        public SystemTime m_stServer;

        [MarshalAs( UnmanagedType.ByValArray , SizeConst = SIZE_MACADDRESS )]
        public char[ ] m_szMACAddress;

        [MarshalAs( UnmanagedType.ByValArray , SizeConst = SIZE_LICENSEKEY )]
        public char[ ] m_szLicenseKey;

        public void Init( )
        {
            m_szUserId = new char[ SIZE_USERID ];
            m_szPassword = new char[ SIZE_PASSWORD ];
            m_Options = new char[ SIZE_OPTIONS ];
            m_stServer = new SystemTime( );
            m_szMACAddress = new char[ SIZE_MACADDRESS ];
            m_szLicenseKey = new char[ SIZE_LICENSEKEY ];
        }

        public LoginStruct( string userId , string password , uint dwMethodType , int nType , uint dwPermission )
        {
            m_szUserId = new char[ SIZE_USERID ];
            m_szPassword = new char[ SIZE_PASSWORD ];
            m_Options = new char[ SIZE_OPTIONS ];
            m_stServer = new SystemTime( );
            m_szMACAddress = new char[ SIZE_MACADDRESS ];
            m_szLicenseKey = new char[ SIZE_LICENSEKEY ];

            m_dwMethodType = dwMethodType;
            m_nMethodAck = 0;
            m_nType = nType;
            m_dwPermission = dwPermission;

            KWConvert.StringTocharArray( userId , ref m_szUserId );
            KWConvert.StringTocharArray( password , ref m_szPassword );
        }

        public void ConverFromByteArray( byte[ ] ba )
        {
            int pos = 0;
            m_dwMethodType = KWConvert.GetUIntFromByteArray( ba , ref pos );
            m_nMethodAck = KWConvert.GetIntFromByteArray( ba , ref pos );
            pos += SIZE_USERID + SIZE_PASSWORD + 4 + 4;
            m_Options = new char[ SIZE_OPTIONS ];
            KWConvert.GetCharArrayFromByteArray( ba , ref m_Options , ref pos , SIZE_OPTIONS );
            pos = 272;
            m_stServer = new SystemTime( );
            KWConvert.GeySystemTimeFromByteArray( ba , ref m_stServer , ref pos );
        }

        public int length( )
        {
            return KWConvert.SizeOf( this );
        }

        public byte[ ] getByteArray( )
        {
            int count = length( );
            byte[ ] byteArr = new byte[ count ];

            //BinaryFormatter bf = new BinaryFormatter();
            //MemoryStream   stream   =   new   MemoryStream();
            //stream.WriteByte(byte)
            //bf.Serialize(stream, this);

            //return stream.GetBuffer();
            int pos = 0;
            byte[ ] bytes;
            bytes = KWConvert.GetByteArrayFromUInt( m_dwMethodType );

            int i;
            for ( i = 0; i < 4; i++ )
            {
                byteArr[ pos ] = bytes[ i ];
                pos++;
            }

            bytes = KWConvert.GetByteArrayFromInt( m_nMethodAck );

            for ( i = 0; i < 4; i++ )
            {
                byteArr[ pos ] = bytes[ i ];
                pos++;
            }
            for ( i = 0; i < SIZE_USERID; i++ )
            {
                byteArr[ pos ] = ( byte )m_szUserId[ i ];
                pos++;
            }
            for ( i = 0; i < SIZE_PASSWORD; i++ )
            {
                byteArr[ pos ] = ( byte )m_szPassword[ i ];
                pos++;
            }

            //bytes = Convert.GetByteArrayFromInt(m_nType);
            //for (i = 0; i < 4; i++)
            //{
            //    byteArr[pos] = bytes[i];
            //    pos++;
            //}
            //bytes = Convert.GetByteArrayFromUInt(m_dwPermission);
            //for (i = 0; i < 4; i++)
            //{
            //    byteArr[pos] = bytes[i];
            //    pos++;
            //}

            byteArr[ 36 ] = ( byte )m_nType;
            byteArr[ 40 ] = ( byte )m_dwPermission;

            pos += SIZE_OPTIONS;

            return byteArr;
        }
    }
}